/**
 * 
 */
package com.antilia.infomaresme.pages;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow.WindowClosedCallback;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.ResourceModel;

import com.antilia.infomaresme.InfomaresmeSession;
import com.antilia.infomaresme.pages.alert.AlertsPage;
import com.antilia.infomaresme.pages.myitems.ListItemsPage;
import com.antilia.infomaresme.pages.signin.LogInContextPanel;
import com.antilia.infomaresme.provider.UserLdm;
import com.antilia.infomaresme.resources.InfoModalWindow;

/**
 * @author Ernesto
 *
 */
public class TopNavigationPanel extends Panel {

	private static final long serialVersionUID = 1L;

	private ModalWindow modal;
	
	private String page = "";
	
	private LogInContextPanel loging;
	
	/**
	 * @param id
	 */
	public TopNavigationPanel(String id) {
		super(id);
		modal = new InfoModalWindow("modal");
		modal.setTitle(new ResourceModel("loginPage"));
		add(modal);
		modal.setWindowClosedCallback(new WindowClosedCallback() {
			
			private static final long serialVersionUID = 1L;

			public void onClose(AjaxRequestTarget target) {
				if(page.equals("ListItemsPage"))
					setResponsePage(new ListItemsPage(new UserLdm(InfomaresmeSession.getSession().getUserId())));
				if(page.equals("AlertsPage"))
					setResponsePage(new AlertsPage(new UserLdm(InfomaresmeSession.getSession().getUserId())));
			}
		});
		AjaxLink<Void> accesoAnuciantes = new AjaxLink<Void>("accesoAnuciantes") {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				if(InfomaresmeSession.getSession().isAuthenticated()) {
					setResponsePage(new ListItemsPage(new UserLdm(InfomaresmeSession.getSession().getUserId())));
					return;
				}
				loging = new LogInContextPanel(modal.getContentId()) {
					
					private static final long serialVersionUID = 1L;

					@Override
					public void onLogIn(AjaxRequestTarget target) {
						page = "ListItemsPage";
						modal.close(target);
					}
				
				};
				modal.setContent(loging);
				modal.show(target);
			}
		};		
		
		
		add(accesoAnuciantes);
		
		AjaxLink<Void> alertas = new AjaxLink<Void>("alertas") {
			
			private static final long serialVersionUID = 1L;

			@Override
			public void onClick(AjaxRequestTarget target) {
				if(InfomaresmeSession.getSession().isAuthenticated()) {
					setResponsePage(new AlertsPage(new UserLdm(InfomaresmeSession.getSession().getUserId())));
					return;
				}
				loging = new LogInContextPanel(modal.getContentId()) {
					
					private static final long serialVersionUID = 1L;

					@Override
					public void onLogIn(AjaxRequestTarget target) {
						page = "AlertsPage";
						modal.close(target);
					}
				
				};
				modal.setContent(loging);
				modal.show(target);
				modal.show(target);
			}
		};		
		add(alertas);
		
	}
}
